/*
 * @Author: ljk
 * @Date: 2023-05-10 21:39:38
 * @LastEditors: ljk
 * @LastEditTime: 2023-05-11 17:46:14
 * @Description: 任务类hpp
 */
#pragma once

#include <iostream>
#include <vector>
#include <unordered_map>
#include <functional>

// typedef std::function<void ()> func_t;

typedef void (*func_t)(); // 函数指针

void Exit()
{
    //此种进程退出的方式也行，这里用管道退出
    //exit(0);
}
void PrintLog()
{
    std::cout << "pid: " << getpid() << ", 打印日志任务，正在被执行..." << std::endl;
}

void InsertMySQL()
{
    std::cout << "pid: " << getpid() << ", 执行数据库任务， 正在被执行..." << std::endl;
}

void NetRequest()
{
    std::cout << "pid: " << getpid() << ", 执行网络请求任务， 正在被执行..." << std::endl;
}

// 约定每一个command必须是4byte
#define COMMAND_EXIT 0
#define COMMAND_LOG 1
#define COMMAND_MYSQL 2
#define COMMAND_REQUEST 3

class Task
{
public:
    Task()
    {
        funcs.push_back(Exit);
        funcs.push_back(PrintLog);
        funcs.push_back(InsertMySQL);
        funcs.push_back(NetRequest);
    }
    void Execute(int command)
    {
        if (command >= 0 && command < funcs.size())
        {
            funcs[command]();
        }
    }
    ~Task()
    {
    }

public:
    std::vector<func_t> funcs;
    // 可以改成map的key-value模型索引找到的方法去调用
    // std::unordered_map<std::string, func_t> funcs;
};